package Thread.ThreadSynchronization;

/*
 * Author: JacksonCoder
 * FileName: SequentialExecutionWithCompletableFuture
 * Version: 1.0.0
 * Date: 2025/1/23
 */

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SequentialExecutionWithCompletableFuture {

    public static void main(String[] args) {

        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        // 使用CompletableFuture 控制任务顺序
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            System.out.println("T1 is running");
        }, executorService);

        CompletableFuture<Void> future2 = future1.thenRunAsync(() -> {
            System.out.println("T2 is running");
        }, executorService);

        CompletableFuture<Void> future3 = future2.thenRunAsync(() -> {
            System.out.println("T3 is running");
        }, executorService);

        // 等待T3完成
        future3.join();
        System.out.println("Threads hava finished execution");
    }

}
